import traceback

import requests
from requests.adapters import HTTPAdapter


class CommunicatorObj:

    def __init__(self, host):
        self.__host = host
        self.timeout = 1

    def move_one(self, pos, value):
        request_body = {
            'api': '/api/move_one',
            'params': {'index': pos, 'angle': value},
            'headers': {'Content-Type': 'application/json'}
        }
        self.__send_request(request_body)

    def reset_arm(self):
        request_body = {
            'api': '/api/reset_arm',
            'params': {},
            'headers': {}
        }
        self.__send_request(request_body)

    def start_study(self):
        request_body = {
            'api': '/api/start_study',
            'params': {},
            'headers': {}
        }
        self.__send_request(request_body)

    def finish_study(self):
        request_body = {
            'api': '/api/finish_study',
            'params': {},
            'headers': {}
        }
        self.__send_request(request_body)

    def get_position(self):
        request_body = {
            'api': '/api/get_position',
            'params': {},
            'headers': {}
        }
        return self.__send_request(request_body)

    def move_all(self, pos_list):
        request_body = {
            'api': '/api/move_all',
            'params': {'position': pos_list},
            'headers': {'Content-Type': 'application/json'}
        }
        self.__send_request(request_body)

    def set_rgb(self, r, g, b):
        rgb = [r, g, b]
        request_body = {
            'api': '/api/set_rgb',
            'params': {'rgb': rgb},
            'headers': {'Content-Type': 'application/json'}
        }
        self.__send_request(request_body)

    def set_pwm(self, index, angle):
        request_body = {
            'api': '/api/set_pwm',
            'params': {'index': index, 'angle': angle},
            'headers': {'Content-Type': 'application/json'}
        }
        self.__send_request(request_body)

    def set_gpio(self, bcm_code, value):
        request_body = {
            'api': '/api/set_gpio',
            'params': {'bcm_code': bcm_code, 'value': value},
            'headers': {'Content-Type': 'application/json'}
        }
        self.__send_request(request_body)

    def read_input(self, input_pin, output_pin):
        request_body = {
            'api': '/api/read_input',
            'params': {'input_pin': input_pin, 'output_pin': output_pin},
            'headers': {'Content-Type': 'application/json'}
        }
        self.__send_request(request_body)

    def step_motor(self, enable_pin, direction_pin, step_pin, enable, direction, step):
        request_body = {
            'api': '/api/step_motor',
            'params': {'enable_pin': enable_pin, 'direction_pin': direction_pin, 'step_pin': step_pin, 'enable': enable,
                       'direction': direction, 'step': step},
            'headers': {'Content-Type': 'application/json'}
        }
        self.set_timeout(step)
        self.__send_request(request_body)
        self.set_timeout(1)

    def __send_request(self, request_body):
        data = request_body['params']
        if not type(data) == bytes:
            if not type(data) == str:
                data = str(data)
            data = data.encode('utf-8')
        s = requests.Session()
        s.mount('http://', HTTPAdapter(max_retries=3))
        url = 'http://' + self.__host + request_body['api']
        try:
            response = s.get(url=url, data=data, headers=request_body['headers'], timeout=self.timeout)
            res = response.text
            # request_json = json.loads(res)
            # print(request_json)
            print(res)
            return res
        except requests.exceptions.ConnectionError:
            print('连接超时')

    def set_timeout(self, timeout):
        self.timeout = timeout


if __name__ == '__main__':
    communicator = CommunicatorObj('192.168.2.117:6666')
    communicator.step_motor(11, 13, 15, 1, 1, 1)
